package com.juyu.miliao.api.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.util.Md5Encryption;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.security.core.Authentication;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.juyu.miliao.api.domain.UserWallet;
import com.juyu.miliao.api.service.UserWalletService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;

@RestController
@RequestMapping("userWallet")
public class UserWalletController {
	@Resource
	private UserWalletService userWalletService;

	@GetMapping("/{userId:^[1-9][0-9]*$}")
	public UserWallet getUserWalletById(HttpServletRequest request, HttpServletResponse response,
			Authentication token, @PathVariable("userId") Integer userId) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != userId)
			throw new IllegalParamException("用户id错误");
		UserWallet userWallet = userWalletService.getUserWalletById(userId);
		//2019-10-12 增加混淆
		CommonHead com = VerifyUtil.verifyCommonHead(request);
		if(!StringUtils.isEmpty(com.getRanReqPk())){
			response.setHeader("ranReqPk",com.getRanReqPk());
			response.setHeader("ranResPk", Md5Encryption.getDigestStr(com.getRanReqPk()));
		}
		return userWallet;
	}
}
